package com.mathdroid.gui.main;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import com.mathdroid.exceptionhandling.DroidClipboard;
import com.mathdroid.gui.ImageLoader;
import com.mathdroid.model.simulation.CombatLog;

/**
 * This panel shows sample combat logs for a completed series of simulations.
 */
public final class CombatLogPanel extends JPanel {

	/**
	 * Default UID.
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * The constraints for this UI components.
	 */
	private final GridBagConstraints c = new GridBagConstraints();
	
	/**
	 * The horizontal weight of the components.
	 */
	private static final double HORIZONTAL_WEIGHT = 10;
	
	/**
	 * The vertical weight of the scroll pane.
	 */
	private static final double HUGE_VERTICAL_WEIGHT = 100;

	/**
	 * The width of the GridBagLayout.
	 */
	private static final int GRID_WIDTH = 3;
	
	/**
	 * The scroll pane for the text area.
	 */
	private final JScrollPane sp = new JScrollPane();
	
	/**
	 * The text area for the combat logs.
	 */
	private final JTextArea ta = new JTextArea();
	
	/**
	 * The combat logs.
	 */
	private LinkedList<CombatLog> logs = null;
	
	/**
	 * Button to copy the content to the clipboard.
	 */
	private final JButton copyToClipboard = new JButton("Copy to Clipboard");
	
	/**
	 * Image loader.
	 */
	private final ImageLoader il = new ImageLoader();
	
	/**
	 * Button to close the frame.
	 */
	private final JButton close = new JButton("Close");
	
	/**
	 * Cycle through the logs.
	 */
	private final JButton nextLog = new JButton("Next Combatlog");
	
	/**
	 * The index of the currently displayed log.
	 */
	private int currentLogIndex = 0;
	
	/**
	 * Parent JFrame.
	 */
	private JFrame parent = null;
	
	/**
	 * Default Constructor.
	 * @param p The parent JFrame.
	 */
	public CombatLogPanel(final JFrame p) {
		this.parent = p;
		this.setLayout(new GridBagLayout());
		this.buildScrollPane();
		this.buildCycleButton();
		this.buildCopyButton();
		this.buildCloseButton();
	}

	/**
	 * Build the button to cycle through the logs.
	 */
	private void buildCycleButton() {
		this.c.fill = GridBagConstraints.HORIZONTAL;
		this.c.gridx = 0;
		this.c.gridy = 1;
		this.c.weightx = HORIZONTAL_WEIGHT;
		this.c.weighty = 0;
		this.c.gridwidth = 1;
		
		this.nextLog.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(final ActionEvent e) {
				cycleThroughLogs();
			}
		});
		
		this.nextLog.setIcon(this.il.getImage("arrow_rotate_clockwise"));
		this.add(this.nextLog, this.c);
	}
	
	/**
	 * Cycle through the combat logs.
	 */
	private void cycleThroughLogs() {
		this.currentLogIndex = (this.currentLogIndex + 1) % this.logs.size();
		this.ta.setText("Combatlog #" + this.currentLogIndex + "\n\n" 
				+ this.logs.get(this.currentLogIndex).toString());
		this.ta.setCaretPosition(0);
	}

	/**
	 * Build the button to close the frame.
	 */
	private void buildCloseButton() {
		this.c.fill = GridBagConstraints.HORIZONTAL;
		this.c.gridx = 2;
		this.c.gridy = 1;
		this.c.weightx = HORIZONTAL_WEIGHT;
		this.c.weighty = 0;
		this.c.gridwidth = 1;
		
		this.close.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(final ActionEvent e) {
				parent.dispose();
			}
		});
		
		this.close.setIcon(this.il.getImage("cross"));
		this.add(this.close, this.c);
	}

	/**
	 * Build the button to copy the content to the clipboard.
	 */
	private void buildCopyButton() {
		this.c.fill = GridBagConstraints.HORIZONTAL;
		this.c.gridx = 1;
		this.c.gridy = 1;
		this.c.weightx = HORIZONTAL_WEIGHT;
		this.c.weighty = 0;
		this.c.gridwidth = 1;
		
		this.copyToClipboard.setIcon(this.il.getImage("clipboard_sign"));
		
		this.copyToClipboard.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(final ActionEvent e) {
				new DroidClipboard().copy(logs.get(0).toString());
			}
		});
		
		this.add(this.copyToClipboard, this.c);
	}

	/**
	 * Build the scroll pane for the table.
	 */
	private void buildScrollPane() {
		this.c.fill = GridBagConstraints.BOTH;
		this.c.gridx = 0;
		this.c.gridy = 0;
		this.c.weightx = HORIZONTAL_WEIGHT;
		this.c.weighty = HUGE_VERTICAL_WEIGHT;
		this.c.gridwidth = GRID_WIDTH;
		
		this.add(this.sp, this.c);
	}
	
	/**
	 * Set the combatlog list.
	 * @param list Combatlog list.
	 */
	public void setCombatLogs(final LinkedList<CombatLog> list) {
		this.logs = list;

		this.ta.setText("Combatlog #0\n\n" + list.get(0).toString());
		this.ta.setCaretPosition(0);
		
		this.sp.setViewportView(this.ta);
	}

}
